package com.yjt.sousou.detail;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yjt.sousou.R;
import com.yjt.sousou.detail.entity.FilterBean;

import java.util.List;

/**
 * 作者：83734
 * 时间：2018/11/27
 * 描述：选择设备 选择部门
 */
public class CommonPop extends PopupWindow {
    private LayoutInflater mInflater;
    private Context mContext;
    private List<FilterBean> lists;
    private ResultListener mListener;
    private String id = "";
    private String name = "";

    public CommonPop(Context context, List<FilterBean> listdata, ResultListener listener) {
        super(context);
        mContext = context;
        this.lists = listdata;
        mInflater = LayoutInflater.from(context);
        mListener = listener;
        View view = LayoutInflater.from(context).inflate(R.layout.pop_select_device,null);
        TextView tv_cancel= view.findViewById(R.id.tv_cancel);
        TextView tv_confirm= view.findViewById(R.id.tv_confirm);
        RecyclerView recyclerView = view.findViewById(R.id.rl_view);
        LinearLayoutManager manager = new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        BaseQuickAdapter adapter = new BaseQuickAdapter<FilterBean,BaseViewHolder>(R.layout.item_my_pop_list,lists) {
            @Override
            protected void convert(BaseViewHolder helper, FilterBean item) {
                helper.setText(R.id.tv_name,item.getName());
                if(item.isClicked()){
                    helper.setTextColor(R.id.tv_name, ContextCompat.getColor(mContext,R.color.blue_bg));
                    id = item.getId();
                    name = item.getName();
                }else {
                    helper.setTextColor(R.id.tv_name,ContextCompat.getColor(mContext,R.color.black));
                }
            }
        };
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (int i = 0; i < lists.size(); i++) {
                    lists.get(i).setClicked(position == i);
                }
                adapter.notifyDataSetChanged();
            }
        });
        int widthPixels = context.getResources().getDisplayMetrics().widthPixels;
        setWidth(4 * widthPixels / 5);
        setHeight(widthPixels);
        setContentView(view);
        setFocusable(true);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setOutsideTouchable(true);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                mListener.dismiss();
            }
        });
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               dismiss();
            }
        });
        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mListener!=null)
                    mListener.res(id,name);
                dismiss();
            }
        });
    }
    public void show(View view){
        showAtLocation(view, Gravity.CENTER, 0, 0);
    }

    public interface ResultListener{
        void res(String id, String name);
        void dismiss();
    }
}
